<?php

namespace App\Model;

use Elasticsearch\ClientBuilder;

class Es
{
    public function __construct()
    {
        $hosts = [
            env('ELASTIC_HOST')
        ];
        $this->client = ClientBuilder::create()
            ->setHosts($hosts)
            ->build();
    }

    public function selectIndex()
    {
        $params = ['index' => env('ES_INDEX')];
        $response = $this->client->indices()->getSettings($params);
        var_dump($response);die;
    }

    public function searchDoc($keyword)
    {
        $params = [];
        $params['index'] = env('ES_INDEX');
        $params['body']['query']['match']['desc'] = $keyword;
        $res = $this->client->search($params);
        return $res['hits']['hits'];

    }
    public function createDoc($desc)
    {
        $params = [
          'index' => env('ES_INDEX'),
          'id' => time(),
          'body' => [
              'num' => time(),
              'desc' => $desc,
          ]
        ];
        $response = $this->client->index($params);
        return $response;
    }

    public function deleteIndex($index)
    {
        $params = [
            'index' => $index
        ];
        $response = $this->client->indices()->delete($params);
        return $response;
    }
    public function createIndex($index)
    {
        $params = [
            'index' => $index,
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,//是数据分片数，默认为5，有时候设置为3
                    'number_of_replicas' => 0, //数据库备份数 如果只有一台机器，设置为0
                    'analysis' => [
                        'filter' => [
                            'my_synonym' => [
                                'type' => 'synonym',
                                'synonyms_path' => 'analysis/synonym.txt'
                            ]
                        ],
                        'analyzer' => [
                            'ik_synonym_smart' => [
                                'type' => 'custom',
                                'tokenizer' => 'ik_smart',
                                'filter' => ['my_synonym']
                            ],
                            'ik_synonym_max' => [
                                'type' => 'custom',
                                'tokenizer' => 'ik_max_word',
                                'filter' => ['my_synonym']
                            ],
                        ]
                    ]
                ],
                'mappings' => [
                    'properties' => [ #指定文档储存的Field属性
                        'num' => [ #申请人
                            'type' => 'integer', # 类型
                        ],
                        'desc' => [
                            'type' => 'text',
//                            'ignore_above'=>256,
                            'analyzer'=>'ik_synonym_max'
                        ]
                    ]
                ]
            ]
        ];
        $response = $this->client->indices()->create($params);
        return $response;
    }

    /**
     * 判断索引是否存在
     * @param $index
     */
    public function isExist($index)
    {
        $params['index'] = $index;
        $bool = $this->client->indices()->exists($params);
        return $bool;
    }

}
